package com.dingli.dao;

import com.dingli.entity.UserLevel;
import com.dingli.until.JdbcTools;

import java.util.List;
import java.util.Map;

public class UserLevelDAO {
    private JdbcTools jdbcTools = new JdbcTools();

    public UserLevel ormUser(Map<String, Object> map) {
        UserLevel userLevel = new UserLevel();
        if (map.get("id") != null) {
            userLevel.setId((Integer) map.get("id"));
        }
        if (map.get("uid") != null) {
            userLevel.setUid((Integer) map.get("uid"));
        }
        if (map.get("level") != null) {
            userLevel.setLevel((Integer) map.get("level"));
        }
        if (map.get("money") != null) {
            userLevel.setLimit((Double) map.get("money"));
        }
        return userLevel;
    }

    public int insert(UserLevel userLevel) {
        String sql = "insert into userlevel(uid,level,money) value(?,?,?)";
        return jdbcTools.insert(sql, userLevel.getUid(), userLevel.getLevel(), userLevel.getLimit());
    }

    public int deleteById(int uid) {
        String sql = "delete from userlevel where uid=?";
        return jdbcTools.delete(sql, uid);

    }

    public UserLevel selectById(int uid) {
        String sql = "select * from userlevel where uid=?";
        List<Map<String, Object>> list = jdbcTools.select(sql, uid);
        if (list.size() == 0) {
            return null;
        }
        return ormUser(list.get(0));
    }
    public  int updateById(UserLevel userLevel) {
        System.out.println(userLevel);
        String sql ="update userlevel set uid=?,level=?,money=?";
        return jdbcTools.update(sql,userLevel.getUid(),userLevel.getLevel(),userLevel.getLimit());
    }

}